import {onMounted, ref} from "vue";
import axios from "@/utils/axios";
import {message} from "ant-design-vue";

export default ()=>{
    const settingMixPlantId = ref(-1);
    const msgUserList = ref(new Array());

    /**
     * 设置用户关联
     * @param mpId 拌合站id
     * @param userId 用户id
     */
    const addMsgUser = (mpId:number = settingMixPlantId.value, userId:number)=>{
        axios
            .post("/mix/user/addMessageUser",{
                mpId:mpId,
                userId:userId
            })
            .then((res:any) => {
                if(res.success) {
                    message.success("设置成功")
                }else{
                    message.error("设置失败")
                }
                getMsgUserList(mpId);
            });
    }

    /**
     * 删除用户关联
     * @param mpId 拌合站id
     * @param userId 用户id
     */
    const delMsgUser = (mpId:number = settingMixPlantId.value, userId:number)=>{
        axios
            .post("/mix/user/delMessageUser",{
                mpId:mpId,
                userId:userId
            })
            .then((res:any) => {
                if(res.success) {
                    message.success("设置成功")
                }else{
                    message.error("设置失败")
                }
                getMsgUserList(mpId);
            });
    }

    /**
     * 获取所有用户及关联关系
     */
    const getMsgUserList = (id:number = settingMixPlantId.value)=>{
        axios
            .post("/mix/user/getALLUserAndMessageUserByMpId",{
                id:id
            })
            .then((res:any) => {
                msgUserList.value = res.data;
            });
    }

    return {
        settingMixPlantId,
        msgUserList,
        addMsgUser,
        delMsgUser,
        getMsgUserList
    }
}